﻿using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEditor;
using UnityEngine;
using Yoozoo.UI;

namespace com.yoozoo.gta.Extension.UGUI.Editor
{
    
    [CustomEditor(typeof(ScreenOrientItem), true)]
    public class ScreenOrientionItemEditor:UnityEditor.Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            
            var item = target as ScreenOrientItem;
            serializedObject.Update();
            
            //EditorGUILayout.PropertyField(serializedObject.FindProperty("m_CanvasScaler"));
            //EditorGUILayout.PropertyField(serializedObject.FindProperty("lastScreenWidth"));
            //EditorGUILayout.PropertyField(serializedObject.FindProperty("lastScreenHeight"));

            if(GUILayout.Button("给所有对象添加Adapter组件"))
            {
                //给所有空的添加Adapter组件
                item.CleanUpAdapter();
                item.InitAllAdapter();
                item.RegisterAllListener();
            }
            
            if(GUILayout.Button("清理不需要的Adapter组件"))
            {
                //当横竖屏属性一致，认为是不需要的Adapter组件   
                item.CleanUpAdapter();
            }
            
            if(GUILayout.Button("刷新收集的Adapter组件"))
            {
                //刷新所有Adapter组件
                item.RefreshCollectUsedListener();
            }
            
            if(GUILayout.Button("清理所有的Adapter组件(一天白干)"))
            {
                //刷新所有Adapter组件
                item.CleanUpAdapter(true);
                ScreenOrientItem.CleanUpByHand = true;
            
            }

            // if (GUILayout.Button("替换window"))
            // {
            //     ReplacePrefab(item.gameObject);
            // }
        }
        
        public void ReplacePrefab(GameObject root)
        {
            var uiExpansion = root.GetComponent<UIExpansion>();

            var item = uiExpansion.GetModuleDatas();
            foreach (var moduleData in item)
            {
                if (moduleData.PrefabPath.Contains("UI/Modules/Windows"))
                {
                    var targetTransform = moduleData.UI.GetComponent<RectTransform>();
                    // 获取windowModule的原始prefab路径
                    string newPrefabPath = moduleData.PrefabPath.Replace("UI/Modules/Windows", "UI/Modules/WindowsPortrait");

                    // 加载新的Prefab
                    GameObject newPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(newPrefabPath);

                    if (newPrefab == null)
                    {
                        Debug.LogError("New prefab not found at path: " + newPrefabPath);
                        return;
                    }

                    // 获取旧Prefab的父对象
                    Transform parent = targetTransform.parent;

                    // 在父对象下实例化新的Prefab
                    GameObject newPrefabInstance = PrefabUtility.InstantiatePrefab(newPrefab) as GameObject;
                    newPrefabInstance.transform.SetParent(parent, false);

                    // 将新的Prefab实例的位置、旋转和缩放设置为旧Prefab的位置、旋转和缩放
                    var newRectTransform = newPrefabInstance.transform.GetComponent<RectTransform>();
                    
                    newRectTransform.anchorMin = targetTransform.anchorMin;
                    newRectTransform.anchorMax = targetTransform.anchorMax;
                    newRectTransform.sizeDelta = targetTransform.sizeDelta;
                    newRectTransform.localPosition = targetTransform.localPosition;
                    newRectTransform.anchoredPosition = targetTransform.anchoredPosition;
                    newRectTransform.localEulerAngles = targetTransform.localEulerAngles;
                    newRectTransform.localScale = targetTransform.localScale;
                    newRectTransform.pivot = targetTransform.pivot;
                    // 销毁旧的Prefab
                    GameObject.DestroyImmediate(targetTransform.gameObject);
                    //moduleData.PrefabPath = newPrefabPath;
                    
                    // 保存更改
                    PrefabUtility.SavePrefabAsset(newPrefabInstance);
                }
            }
        }
    }
}